package com.al.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.al.classs.User;

import net.sf.json.JSONObject;

@WebServlet("/test")
public class TestServlet extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		/*
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		String str = "Hello World";
		resp.getWriter().write(str);
//		req.setAttribute("str", str);
//		req.getRequestDispatcher("/test.jsp").forward(req, resp);
 */
		User user = new User(1, "张三", 89.2);
		// 将 Java 对象 转换成 JSON 格式（需要导 jar 包）
		JSONObject jsonObject = JSONObject.fromObject(user);
		resp.setCharacterEncoding("UTF-8");
		resp.getWriter().write(jsonObject.toString());
	}
}
